﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace Wpf_Examples.Tools
{
    public class LanguageChangeHelper
    {
        private static readonly Dictionary<string, CultureInfo> _supportedCultures = new Dictionary<string, CultureInfo>
    {
        { "en", new CultureInfo("en-US") },
        { "Japan", new CultureInfo("jp-JP") },
        { "French", new CultureInfo("fr-FR") },
        { "zh-CN", new CultureInfo("zh-CN") }
    };

        public static void SetLanguage(string languageCode)
        {
            if (_supportedCultures.TryGetValue(languageCode, out CultureInfo cultureInfo))
            {
                Thread.CurrentThread.CurrentCulture = cultureInfo;
                Thread.CurrentThread.CurrentUICulture = cultureInfo;

                // 重新加载资源
                ResourceDictionary resourceDict = new ResourceDictionary();
                resourceDict.Source = new Uri($"pack://application:,,,/YourAssembly;component/Resources/{languageCode}.xaml");
                Application.Current.Resources.MergedDictionaries.Clear();
                Application.Current.Resources.MergedDictionaries.Add(resourceDict);

                // 刷新 UI（可选）
                foreach (Window window in Application.Current.Windows)
                {
                    window.Content = null;
                    window.Content = window.Content;
                }
            }
        }

        public static string GetSupportedLanguages()
        {
            return string.Join(", ", _supportedCultures.Keys);
        }
    }
}
